<?php

/**
 * This is the model class for table "hist_claim".
 *
 * The followings are the available columns in table 'hist_claim':
 * @property integer $id
 * @property string $nota_debet_no
 * @property string $policy_no
 * @property string $trx_date
 * @property double $nominal
 * @property string $status
 * @property string $remarks
 * @property integer $createdby
 * @property string $createdtime
 * @property integer $updatedby
 * @property string $updatedtime
 */
class HistClaim extends CActiveRecord
{
	
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'hist_claim';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('policy_no', 'required'),
			array('createdby, updatedby, mst_client_id, reg_policy_id', 'numerical', 'integerOnly'=>true),
			array('nominal', 'numerical'),
			array('nota_debet_no, policy_no', 'length', 'max'=>50),
			array('status, type_reg_policy', 'length', 'max'=>1),
			array('remarks', 'length', 'max'=>100),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, mst_client_id, reg_policy_id, type_reg_policy, nota_debet_no, policy_no, trx_date, nominal, status, remarks, createdby, createdtime, updatedby, updatedtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nota_debet_no' => 'Nota Debet No',
			'policy_no' => 'Policy No',
			'trx_date' => 'Trx Date',
			'nominal' => 'Nominal',
			'status' => 'Status',
			'remarks' => 'Remarks',
			'createdby' => 'Tgl Input',
			'createdtime' => 'Createdtime',
			'updatedby' => 'Updatedby',
			'updatedtime' => 'Updatedtime',
			'mst_client_id' => 'mst_client_id',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->order='createdtime desc';
		
		if(!empty($this->nota_debet_no) && empty($this->createdtime)){
		$criteria->join = 'join mst_client a on t.mst_client_id=a.id';
		$criteria->condition = "lower(a.name) like '%".strtolower($this->nota_debet_no)."%'";
		}
		
		if(!empty($this->createdtime) && !empty($this->nota_debet_no)){
		$criteria->join = 'join mst_client a on t.mst_client_id=a.id';
		$criteria->condition = "lower(a.name) like '%".strtolower($this->nota_debet_no)."%' and to_char(t.createdtime,'YYYY-MM-DD')='".date('Y-m-d',strtotime($this->createdtime))."'";
		}
		
		if(!empty($this->createdtime) && empty($this->nota_debet_no)){
		$criteria->condition = "to_char(createdtime,'YYYY-MM-DD')='".date('Y-m-d',strtotime($this->createdtime))."'";
		}
		
		

		$criteria->compare('id',$this->id);
		$criteria->compare('policy_no',$this->policy_no,true);
		$criteria->compare('trx_date',$this->trx_date,true);
		$criteria->compare('nominal',$this->nominal);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('remarks',$this->remarks,true);
		$criteria->compare('createdby',$this->createdby);
		$criteria->compare('updatedby',$this->updatedby);
		$criteria->compare('updatedtime',$this->updatedtime,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return HistClaim the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}


	public function hitungTotalKlaim($id_hist_claim){

		$sql='select sum(nominal) as nominal from hist_claim_anggota where id_hist_claim='.$id_hist_claim;

		$data=Yii::app()->db->createCommand($sql)->queryRow();
		
		return $data['nominal'];
	}


	public function getPeriodePolis($reg_policy_id, $type_reg_policy){

		if($type_reg_policy=='1'){
			$startPeriode = RegHealthPolicy::model()->findByPk($reg_policy_id)->start_periode;
			$endPeriode = RegHealthPolicy::model()->findByPk($reg_policy_id)->end_periode;
		}
		else{
			$startPeriode = RegHealthPolicyNon::model()->findByPk($reg_policy_id)->start_periode;
			$endPeriode = RegHealthPolicyNon::model()->findByPk($reg_policy_id)->end_periode;
		}
		
		$periodePolis = date('d-m-Y',strtotime($startPeriode))." s/d ".date('d-m-Y',strtotime($endPeriode));
		return $periodePolis;
	}
}
